<template>
  <div class="xtx-city" ref="target">
    <div class="select" @click="clickStatus" :class="{active:displayState}">
      <span class="placeholder" v-if="!fullLocation">{{placeholder}}</span>
      <span class="value" v-else>{{fullLocation}}</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-if="displayState">
      <div class="loading" v-if="loading"></div>
      <template v-else>
      <span class="ellipsis" v-for="item in currList" :key="item.code" @click='clickCityName(item)'>{{item.name}}</span>
      </template>
    </div>
  </div>
</template>

<script>
import { computed, reactive, ref } from 'vue'
import axios from 'axios'
import { onClickOutside } from '@vueuse/core'
export default {
  name: 'XtxCity',
  props: {
    fullLocation: {
      type: String,
      default: ''

    },
    placeholder:{
      type:String,
      default:'请选择配送地址'
    }
  },
  setup (props, { emit }) {
    // 显示隐藏状态
    const displayState = ref(false)
    const target = ref()
    const allCityInfor = ref([])
    // 加载动画
    const loading = ref(false)
    const open = () => {
      for (const key in citylist) {
        citylist[key] = ''
      }
      displayState.value = true
      loading.value = true
      getCityData().then(res => {
        // loading.value = true

        allCityInfor.value = res
        loading.value = false
      })
    }
    const close = () => {
      displayState.value = false
    }
    const clickStatus = () => {
      displayState.value ? close() : open()
    }
    // onClickOutside点击区域外部
    onClickOutside(target, () => close())
    // 获取城市地址信息
    const getCityData = () => {
      return new Promise((resolve, reject) => {
        if (window.cityData) {
          resolve(window.cityData)
        } else {
          const url = 'https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json'
          axios.get(url).then(res => {
            console.log(resolve)
            window.cityData = res.data
            resolve(res.data)
          })
        }
      })
    }
    //
    const currList = computed(() => {
      // 默认省级名称
      let list = allCityInfor.value
      console.log(list)
      if (citylist.provinceCode && citylist.provinceName) {
        list = list.find(a => a.code === citylist.provinceCode).areaList
      }
      if (citylist.cityCode && citylist.cityName) {
        list = list.find(b => b.code === citylist.cityCode).areaList
      }
      return list
    })
    // 定义得到的城市名字和code
    const citylist = reactive({
      // 省
      provinceCode: '', // 省编码
      provinceName: '', // 省名字
      // 市
      cityCode: '',
      cityName: '',
      // 区
      countyCode: '',
      countyName: '',
      fullLocation: ''
    })

    // 点击获取城市信息
    const clickCityName = (acquire) => {
      console.log(citylist)
      if (acquire.level === 0) {
        // eslint-disable-next-line no-unused-expressions
        citylist.provinceCode = acquire.code
        citylist.provinceName = acquire.name
      }
      if (acquire.level === 1) {
        // eslint-disable-next-line no-unused-expressions
        citylist.cityCode = acquire.code
        citylist.cityName = acquire.name
      }
      if (acquire.level === 2) {
        // eslint-disable-next-line no-unused-expressions
        citylist.countyCode = acquire.code
        citylist.countyName = acquire.name
        citylist.fullLocation = `${citylist.provinceName} ${citylist.cityName} ${citylist.countyName}`
        console.log(currList)
        close()
        emit('changeCity', citylist)
      }
      // console.log(acquire)
    }
    return { displayState, clickStatus, target, currList, loading, clickCityName }
  }

}

</script>

<style scoped lang="less">
.xtx-city{
     display: inline-block;
  position: relative;
  z-index: 400;
  .select{
     border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .placeholder {
      color: #999;
    }
       .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
      .loading {
      height: 290px;
      width: 100%;
      background: url(../../assets/images/loading.gif) no-repeat center;
    }
  }
}
</style>
